<?php 
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/fileuploader/fileuploader.js', CClientScript::POS_HEAD);
Yii::app()->clientScript->registerCssFile(Yii::app()->baseUrl . '/js/fileuploader/fileuploader.css');
?>
<div class="flexform">
    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'product-base-form',
        'enableAjaxValidation' => false
            ));
    ?>
    <div class="row">
        <?php echo $form->labelEx($model, 'p_game'); ?>
        <?php echo $form->dropDownList($model, 'p_game', array('' => '') + ProductCategory::model()->getGameSelect(), array('onchange' => 'getAttr()')); ?>
        <?php echo $form->error($model, 'p_game', array('class' => 'form-msg-error-advanced')); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'p_type'); ?>
        <?php echo $form->dropDownList($model, 'p_type', array('' => '') + $this->model_array, array('onchange' => 'getAttr()')); ?>
        <?php echo $form->error($model, 'p_type', array('class' => 'form-msg-error-advanced')); ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'p_name'); ?>
        <?php echo $form->textField($model, 'p_name', array('size' => 30, 'maxlength' => 150)); ?>
        <?php echo $form->error($model, 'p_name', array('class' => 'form-msg-error-advanced')); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'p_price'); ?>
        <?php echo $form->textField($model, 'p_price', array('size' => 10, 'maxlength' => 10)); ?>
        <?php echo $form->error($model, 'p_price', array('class' => 'form-msg-error-advanced')); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'p_quntity'); ?>
        <?php echo $form->textField($model, 'p_quntity'); ?>
        <?php echo $form->error($model, 'p_quntity', array('class' => 'form-msg-error-advanced')); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'p_rebate'); ?>
        <?php echo $form->textField($model, 'p_rebate'); ?>
        <?php echo $form->error($model, 'p_rebate', array('class' => 'form-msg-error-advanced')); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'p_pro'); ?>
        <?php echo $form->dropDownList($model, 'p_pro', $model->p_pro_array); ?>
        <?php echo $form->error($model, 'p_pro', array('class' => 'form-msg-error-advanced')); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model, 'p_img'); ?>
        <?php echo $form->textField($model, 'p_img', array('size' => 60, 'maxlength' => 225)); ?>
        <?php echo $form->error($model, 'p_img', array('class' => 'form-msg-error-advanced')); ?>
        <div id="upload-file" style="float: left;">
            <noscript>          
            <p>Please enable JavaScript to use file uploader.</p>
            </noscript>   
        </div>
    </div>
    <!--属性-->
    <div id="pAttr" class="row">
        <?php 
        if($model->p_type)
        {
            echo ProductAttribute::model()->getAttrInput($model->p_game, $model->p_type, $model->id);
        }
        ?>
    </div>
    <div class="row">
        <?php echo $form->labelEx($model, 'p_note'); ?>
        <div style="padding-left: 130px;">
            <?php
            $this->widget('application.extensions.ckeditor.CKEditor', array(
                "model" => $model, # Data-Model
                "attribute" => 'p_note', # Attribute in the Data-Model
                "width" => '90%',
            ));
            ?>
        </div>

        <?php echo $form->error($model, 'p_note'); ?>
    </div>
    <div class="action">
        <?php echo CHtml::submitButton($model->isNewRecord ? '创建' : '保存'); ?>
    </div>
    <?php $this->endWidget(); ?>
</div><!-- form -->
<div class="clear"></div>
<script>
    function add(obj){
        var html = $(obj).parent("div").html();
        $("<div style='margin-bottom:5px;margin-left:125px;'>" + html + "<div class='clear'></div></div>").insertAfter($(obj).parent());
    }
    function getAttr(){
        if($("#ProductBase_p_game").val() && $("#ProductBase_p_type").val()){
            $("#pAttr").html("产品属性加载中...");
            $.post("<?php echo $this->createUrl('productAttribute/ajax');?>", {action:"getAttr", game:$("#ProductBase_p_game").val(), type:$("#ProductBase_p_type").val()}, function(data){
                $("#pAttr").html(data);
            });
        }
    }
    
    //file upload
    var upimg='';
    var img_ext = "|jpg|jpeg|png|gif|bmp|";
    function createUploader(elementObj, setValueObj){//   elementObj: DIV, setValueObj: Will set value INPUT    
        var uploader = new qq.FileUploader({
            element: elementObj,
            action: '<?php echo Yii::app()->baseUrl; ?>/fileUpload.php',
            
            debug: false,
            showMessage: function(message){ alert(message); },
            allowedExtensions: ['jpg', 'jpeg', 'png', 'gif', 'bmp'],
            onProgress: function(id, fileName, loaded, total){
                //location.reload();
            },
            onComplete:function(id, fileName, responseJSON){
                if(responseJSON.success){
                    var dot_index = responseJSON.url.lastIndexOf('.');
                    var ext = responseJSON.url.substring(dot_index+1);
                    if(img_ext.indexOf(ext)>0){
                        //setValueObj.value=responseJSON.url;
                        setValueObj.val(responseJSON.url);
                    }
                }
            }
        });
        uploader.setParams({dir:"productImg"});
    }
    $(window).load(function(){
        createUploader(document.getElementById("upload-file"), $("#ProductBase_p_img"));
    });
</script>